package it.polimi.swimv2.entities;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

/**
 * 
 * Entity implementation class for Entity: HelpRequest
 *
 */
@Entity
public class HelpRequest extends Request {
	@Id
	@GeneratedValue
	@Column(name = "Id", nullable = false)
	private Integer id;
	@ManyToOne
	private Ability requiredAbility;
	private String object;
	private boolean feedbackAvailable;
	
	@OneToMany(/*fetch=FetchType.EAGER,*/ mappedBy = "helpRequest",cascade=CascadeType.ALL)
	@LazyCollection(LazyCollectionOption.FALSE)
	@OrderBy("date DESC")
	private List<Message> messages;
	
	/**
	 * Default constructor.
	 */
	public HelpRequest(){
		this.messages=new ArrayList<Message>();
	}
	
	/**
	 * Constructor.
	 * @param usender
	 * @param ureceiver
	 * @param reqAbility
	 * @param object
	 */
	public HelpRequest(User usender, User ureceiver, Ability reqAbility,String object) {
		super(usender,ureceiver);
		this.requiredAbility=reqAbility;
		this.object=object;
		this.messages=new ArrayList<Message>();
		
	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @return the requiredAbility
	 */
	public Ability getRequiredAbility() {
		return requiredAbility;
	}

	/**
	 * @param requiredAbility
	 *            the requiredAbility to set
	 */
	public void setRequiredAbility(Ability requiredAbility) {
		this.requiredAbility = requiredAbility;
	}

	/**
	 * @return the object
	 */
	public String getObject() {
		return object;
	}

	/**
	 * @param object
	 *            the object to set
	 */
	public void setObject(String object) {
		this.object = object;
	}

	/**
	 * @return the feedbackAvailable
	 */
	public boolean isFeedbackAvailable() {
		return feedbackAvailable;
	}

	/**
	 * @param feedbackAvailable
	 *            the feedbackAvailable to set
	 */
	public void setFeedbackAvailable(boolean feedbackAvailable) {
		this.feedbackAvailable = feedbackAvailable;
	}

	/**
	 * @return the messages
	 */
	public Collection<Message> getMessages() {
		return new ArrayList<Message>(messages);
	}
	
	public void addMessage(Message m){
		m.setHelpRequest(this);
		messages.add(m);
	}
	
}
